package controller;
import java.util.GregorianCalendar;
import java.util.HashMap;
import DAO.DAOFactoryRDBMS;
import DAO.DatabasePool;
import DAO.ENSequenceDAO;
import model.ConnectionProperty;
import model.DIDate;
import model.ENSequence;
import model.FTConceptTime;
import rdbms.RDBMSDIConceptDomainDAO;
import rdbms.RDBMSDIDateDAO;
import rdbms.RDBMSFTConceptTimeDAO;

public class Task {

	Term term = new Term();
	private int date;

	HashMap<String, Term> map = new HashMap<String, Term>();

	public Task() throws Exception {
	 
		GregorianCalendar calendar = new GregorianCalendar();
		//Calendar calendar = Calendar.getInstance();  
			
		ConnectionProperty conn;
		String dbId = "";
		String jobId = "DIDATE";
		String connectionId = "1";

		conn = new ConnectionProperty(jobId, connectionId,
				TestUtil.Drivers.POSTGRE.getDriver(),
				TestUtil.Drivers.POSTGRE.getUrl(),
				TestUtil.Drivers.POSTGRE.getUser(),
				TestUtil.Drivers.POSTGRE.getPassword(),
				TestUtil.Drivers.POSTGRE.getSchema());
		DatabasePool.create(conn);

		dbId = conn.getId();

		DAOFactoryRDBMS rdbmsFactory = DAOFactoryRDBMS
				.getDAOFactory(DAOFactoryRDBMS.FACTORY_TYPE.GENERIC_RDBMS);

		ENSequenceDAO sequence = rdbmsFactory.getENSequenceDAO(dbId);

		RDBMSDIDateDAO dateDAO = new RDBMSDIDateDAO(dbId,
				sequence);
		date = sequence.nextval(ENSequence.DATE_ID);
		
		dateDAO.insert(new DIDate(date ,calendar.get(calendar.DAY_OF_MONTH),(calendar.MONTH+1), (calendar.get(calendar.YEAR))));
		System.out.println((calendar.get(calendar.DAY_OF_MONTH)+" "+(calendar.MONTH+3)+" "+(calendar.get(calendar.YEAR))));
		//System.exit(0);
		
		
	}

	public int getDate() {
		return date;
	}

	public void setDate(int date) {
		this.date = date;
	}

	public HashMap getMap() {
		return map;
	}

	public void loadingList() throws Exception {

		ConnectionProperty conn;
		String dbId = "";
		String jobId = "FTEntityDAOTest";
		String connectionId = "1";

		conn = new ConnectionProperty(jobId, connectionId,
				TestUtil.Drivers.POSTGRE.getDriver(),
				TestUtil.Drivers.POSTGRE.getUrl(),
				TestUtil.Drivers.POSTGRE.getUser(),
				TestUtil.Drivers.POSTGRE.getPassword(),
				TestUtil.Drivers.POSTGRE.getSchema());
		DatabasePool.create(conn);

		dbId = conn.getId();

		DAOFactoryRDBMS rdbmsFactory = DAOFactoryRDBMS
				.getDAOFactory(DAOFactoryRDBMS.FACTORY_TYPE.GENERIC_RDBMS);

		ENSequenceDAO sequence = rdbmsFactory.getENSequenceDAO(dbId);

		RDBMSDIConceptDomainDAO cdDAO = new RDBMSDIConceptDomainDAO(dbId,
				sequence);
		
		map = cdDAO.list();
	}

	
	public synchronized Job getJob() {
		Term source;
		try {
			for (String key : map.keySet()) {
				source = map.get(key);
				map.remove(key);
				System.out.println(date);
				return new Job(source, map.clone(), date);
			}
			return null;
		} catch (Exception ex) {
			return null;
		}
	}

	public void generateFrequency() throws Exception {
		Search search = new Search();
		
		ConnectionProperty conn;
		String dbId = "";
		String jobId = "FT_CONECPT_TIME";
		String connectionId = "2";

		conn = new ConnectionProperty(jobId, connectionId,
				TestUtil.Drivers.POSTGRE.getDriver(),
				TestUtil.Drivers.POSTGRE.getUrl(),
				TestUtil.Drivers.POSTGRE.getUser(),
				TestUtil.Drivers.POSTGRE.getPassword(),
				TestUtil.Drivers.POSTGRE.getSchema());
		DatabasePool.create(conn);

		dbId = conn.getId();

		RDBMSFTConceptTimeDAO ctDAO = new RDBMSFTConceptTimeDAO(dbId);

		for (String key : map.keySet()) {
			map.get(key).setFrequency(search.generateFrequency(map.get(key).getDescription()));
			ctDAO.insert(new FTConceptTime(map.get(key).getId(), 1, this.date, map.get(key).getFrequency()));
			System.out.println(key);
		}	
	}	
}
